package com.fuqi.android.zhongcai.base.mvp.presenter;


import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.base.mvp.view.ILoginView;
import com.fuqi.android.zhongcai.base.request.api.LoginApi;
import com.fuqi.android.zhongcai.sql.User;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;
import com.fuqi.framework.utils.NetworkUtils;


/**
 * Presenter层（负责View层和Model层之间的交互和控制业务逻辑）
 *
 * @author lijun
 */
public class LoginPresenterImpl implements LoginPresenterInter {

    private ILoginView mainView;

    public LoginPresenterImpl(ILoginView mainView) {
        this.mainView = mainView;
    }

    @Override
    public void login(String username, String password) {

        if(TextUtils.isEmpty(username)){

            ActivityUtils.toast("请输入手机号");
            mainView.loginException();
        }else if(TextUtils.isEmpty(password)){
            ActivityUtils.toast("请输入密码");
            mainView.loginException();
        }else {

            RequestManager.getInstance().call(new LoginApi(new LoginApi.LoginParams(username, password, NetworkUtils.getImei((Activity)mainView)), respLoginListener, errorLoginListener));
        }
    }


    Response.Listener<User> respLoginListener = new Response.Listener<User>() {

        @Override
        public void onResponse(User response) {

            if (HandleCode.requestSuccess()) {
                Log.i("juner", "juner respListener response : ");
                mainView.loginSuccess(response);
            } else {

                mainView.loginException();
            }
        }
    };


    Response.ErrorListener errorLoginListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            mainView.loginFailed();
            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());

        }
    };


}
